import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait

from Common.flowname import FLOWNAME
from Common.read_xlsx import Read_xlsx
from Common.sqltool import SqlTools
from pages.utils import GetDriver


# 对页面内元素进行分装
class BasesPage():
    _SQL = None

    def __init__(self, model='web'):
        if model == "web":
            self.driver = GetDriver.get_web_driver()
        elif model == "app":
            self.driver = GetDriver.get_app_driver()

    def el_find_element(self, method, location, timeout=300, poll=10):

        if method == "XPATH":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(By.XPATH, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "ID":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(By.ID, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "NAME":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(By.NAME, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "LINK_TEXT":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(By.LINK_TEXT, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "TAG_NAME":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(By.TAG_NAME, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "PARTIAL_LINK_TEXT":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(
                    lambda x: x.find_element(By.PARTIAL_LINK_TEXT, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "CSS_SELECTOR":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(
                    lambda x: x.find_element(By.CSS_SELECTOR, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "CLASS_NAME":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(By.CLASS_NAME, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")

    def el_find_elements(self, method, location, timeout=300, poll=5):
        if method == "XPATH":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(By.XPATH, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "ID":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(By.ID, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "NAME":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(By.NAME, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "LINK_TEXT":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(By.LINK_TEXT, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "TAG_NAME":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(By.TAG_NAME, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "PARTIAL_LINK_TEXT":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(
                    lambda x: x.find_elements(By.PARTIAL_LINK_TEXT, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "CSS_SELECTOR":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(
                    lambda x: x.find_elements(By.CSS_SELECTOR, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")
        elif method == "CLASS_NAME":
            try:
                el = WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_elements(By.CLASS_NAME, location))
                return el
            except Exception as Exc:
                print(f"错误原因是{Exc},出错的位置是{location}")

    def demo1(self, xlsxname):

        # (By.XPATH, "//*[@placeholder='请输入用户名']")

        find_type, find_method, find_location, find_input, find_assert = Read_xlsx(xlsxname)
        for i in range(len(find_type)):
            if find_type[i] == "输入操作":
                self.el_find_element(find_method[i], find_location[i]).send_keys(find_input[i])

            elif find_type[i] == "点击操作":
                if find_type[i] in ["LINK_TEXT", "PARTIAL_LINK_TEXT"]:
                    WINDOWS = self.driver.current_window_handle
                self.el_find_element(find_method[i], find_location[i]).click()
            elif find_type[i] == "选择流程":
                a = 0
                for c in self.el_find_elements(find_method[i], find_location[i]):
                    if a == FLOWNAME.get(find_input[i]):
                        self.el_find_element(find_method[c], find_location[c]).click()
                        break
                    a += 1

            elif find_type[i] == "刷新页面":
                self.driver.refresh()

            elif find_type[i] == "清除cookie":
                self.driver.delete_all_cookies()

            elif find_type[i] == "进入iframe":
                self.driver.switch_to.frame(self.el_find_element(find_method[i], find_location[i]))

            elif find_type[i] == "切换句柄":
                try:
                    self.driver.switch_to.window(WINDOWS)
                except:
                    print("前面的操作中未获取到页面的句柄")

            elif find_type[i] == "下拉框选项":
                depts = Select(self.el_find_element(find_method[i], find_location[i]))
                depts.select_by_value(find_input)

            elif find_type[i] == "SQL查询":
                _SQL = SqlTools().input_sql(find_input[i])
                # 查询结果得返回出去

            elif find_type[i] == "页面前进":
                self.driver.forward()

            elif find_type[i] == "页面后退":
                self.driver.back()

            elif find_type[i] == "暂停一会":
                time.sleep(find_input[i])
